package com.xl.minyue.tc2.dao;


import com.alibaba.fastjson.JSON;
import com.xl.minyue.tc2.mapper.*;
import com.xl.minyue.tc2.pojo.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.swing.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class ReportFormDao {
    @Resource
    private TagInfoMapper tagInfoMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private PointInfoMapper pointInfoMapper;
    @Resource
    private TagDataX3Mapper tagDataX3Mapper;
    @Resource
    private PointOrderMapper orderMapper;

//(一)PI实时数据

    //查询最新数据列表
    public List<TagInfo> getNewData(){
        Map map2 = new HashMap();
        map2.put("comment",1);
        map2.put("status","new");
       return tagInfoMapper.selectTaginfo3(map2);
    }


    //更新所有的status--------------taginfo----------实时数据状态
    public boolean updateStatus(){
        Map map2 = new HashMap();
        map2.put("comment",1);
        //List<TagInfo> list = tagInfoMapper.findAllTaginfo();
        int count2 = tagInfoMapper.checkCount2(map2);
        if (count2==0){
            return true;
        }else{
            map2.put("status","new");
            List<TagInfo> list1 = tagInfoMapper.selectTaginfo3(map2);
            boolean f=false;
            for (TagInfo t:list1) {
                //String status = t.getStatus();
                //if (status !=null){
                Map<String, Object> map = new HashMap<>();
                map.put("status","");
                map.put("id",t.getId());
                int up = tagInfoMapper.updateStatus(map);
                if(up>0){
                    f=true;
                }else{
                    f=false;
                }
               // }
            }
            return f;
        }
    }

    //根据code查询点的信息-----------实时数据的点------------此方法只适用于实时数据的点
    public PointInfo getPointNameToTagName2(String code) {
        Map map = new HashMap<>();
        map.put("code",code);
        map.put("comment",1);
        map.put("kind1","实时");
        return pointInfoMapper.getPointNameByCode(map);
    }

    //根据分类查询点信息2
    public List<PointInfo> checkPointInfoByKind2(String kind2, String kind1){
        Map map = new HashMap<>();
        map.put("kind2",kind2);
        map.put("comment",1);
        map.put("kind1",kind1);
        return pointInfoMapper.findPointByKind2(map);
    }

    //插入点--------pointinfo
    public Boolean addPoints(PointInfo p){
        int b = pointInfoMapper.insert(p);
        return b>0;
    }

    public List<TagDataX3> showTagNewDataByKind2(String kind1, String kind2, String time) {
        Map map = new HashMap();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("dateTime2",time);
        map.put("comment",1);
        return tagDataX3Mapper.showNewDataByKind(map);
    }


    public boolean deleTagDataX3(String kind1, String month) {
        Map map = new HashMap();
        map.put("kind1",kind1);
        map.put("dateTime2",month);
        map.put("comment",0);
        int x3 = tagDataX3Mapper.deleTagDataX3(map);
        return x3>0;
    }

    public List<TagDataX3> showMonthDataByName(String startTime, String endTime, String name, String kind1) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM");
        map.put("endDate", d.parse(endTime));
        map.put("startDate", d.parse(startTime));
        map.put("name",name);
        map.put("kind1",kind1);
        map.put("comment",1);
        return tagDataX3Mapper.showMonthDataByName(map);

    }

    //添加tagInfo---------实时数据
    public boolean addData(TagInfo tagInfo){
        int insert = tagInfoMapper.insert(tagInfo);
        return insert >0;
    }

    //获取一个点的当天中最新的一条数据
   /* public TagInfo getDataByPointAndDate(String tagName,String date){
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("status","new");
        map.put("comment",1);
        TagInfo tagInfo = tagInfoMapper.selectTaginfo(map);
        if(tagInfo !=null){
            return tagInfo;
        }
        return null;
    }*/

    //获取一个点的当天中最新的一条数据
    public List<TagInfo> getDataByPointAndDate3(String tagName,String date){
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("status","new");
        map.put("comment",1);
        List<TagInfo> list = tagInfoMapper.selectTaginfo(map);

        return list;
    }



    public int checkCountByCode(String tagName,String date){
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("status","new");
        map.put("comment",1);
        int count = tagInfoMapper.selectCountByCode(map);
        System.out.println(count+"<------------count");
        return count;
    }

    //获取某一个点的某一天的数据数据列表
    public List<TagInfo> getDataByPointAndDate2(String tagName,String date){
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("comment",1);
        return tagInfoMapper.selectTaginfo2(map);
    }
    public List<TagInfo> getDataByPointAndDate2_(String tagName, String date){
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("comment",1);
        return tagInfoMapper.selectTaginfo2(map);
    }

    //根据分类查询点信息---------针对实时数据
    public List<PointInfo> checkPointInfoByKind(String kind1,String kind2){
        Map map = new HashMap<>();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("comment",1);
        return pointInfoMapper.findPointByKind2(map);
    }

    //删除点信息
    public Boolean delePoints(String id) {
        Map map = new HashMap<>();
        map.put("comment",0);
        map.put("id",id);
        int delePoint = pointInfoMapper.delePoint(map);
        return  delePoint>0;
    }

    //(1)插入日报数据
    public boolean insertDataFromX3Daily(TagDataX3 r){
        int insert = tagDataX3Mapper.insert(r);
        return insert>0;
    }
    public int insertDataFromX3Daily2(TagDataX3 r) {
         return  tagDataX3Mapper.insert2(r);
    }

    //根据code(神华编码)和noteh和kind1查询点的信息
    public PointInfo checkInfoByNoteAndCode(String code, String note,String kind1) {
        Map<String, Object> map = new HashMap<>();
        map.put("pointcode",code);
        map.put("note",note);
        map.put("kind1",kind1);
        map.put("comment",1);
        return pointInfoMapper.checkInfoByNoteAndCode(map);
    }

    //根据指标的中文意思查询指标数据--------日报
    public List<TagDataX3> showDailyDataByName(String endDate, String startDate, String name) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        map.put("endDate", d.parse(endDate));
        map.put("startDate", d.parse(startDate));
        map.put("name",name);
        map.put("comment",1);
        return tagDataX3Mapper.showDataByName(map);
    }


//调整数据展示顺序
    //判断pointorder表格中在kind1和kind2下是否有数据
    public List<PointOrder> checkPointOrderByKind(String kind1, String kind2) {
        Map map = new HashMap<>();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("comment",1);
        return orderMapper.findOrderDataByKind(map);
    }

    public boolean isExistOfPointOrder(String kind1, String kind2, String note, String pointcode) {
        Map map = new HashMap<>();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("comment",1);
        map.put("note",note);
        map.put("pointcode",pointcode);
        int count=orderMapper.isExistOfPointOrder(map);
        return count>0;
    }

    public boolean upOrder(PointOrder p) {
        int count=orderMapper.updateOrder(p);
        return count>0;
    }

    public boolean addOrder(PointOrder p) {
        int count=orderMapper.insert(p);
        return count>0;
    }

    public List<TagDataX3> findOneData(String kind1, String kind2, String code, String note, String time) {
        Map map = new HashMap();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("tagCode",code);
        map.put("comment",1);
        map.put("tagName2",note);
        map.put("dateTime2",time);
        return tagDataX3Mapper.findOneData(map);
    }

    public TagDataX3 findOneData2(String kind1, String kind2, String code, String note, String time) {
        Map map = new HashMap();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("tagCode",code);
        map.put("comment",1);
        map.put("tagName2",note);
        map.put("dateTime2",time);
        return tagDataX3Mapper.findOneData2(map);
    }

    //测试
    //查询所有用户
    public  List<User> findAllUsers(){
        return userMapper.findAll();
    }

    public List<TagInfo> gettimeList(String code, String day) {
        Map map = new HashMap();
        map.put("dateTime",day);
        map.put("tagName",code);
        map.put("comment",1);
       return tagInfoMapper.selectOne(map);

    }

    public List<TagInfo> getDataByPointAndDate2_2(String tagName, String date, String colltime) {
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("comment",1);
        map.put("collTime",colltime);
        return tagInfoMapper.selectTaginfo4(map);


    }

    public List<String> getDateList(String kind1, String kind2) {
        Map map = new HashMap<>();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        List<TagDataX3> dateList = tagDataX3Mapper.findDateList(map);
        List<String> list = new ArrayList<>();
        if(dateList.size()>0){
            for (TagDataX3 t:dateList) {
                if(t !=null){
                    if(!list.contains(t.getDatetime2())){
                        list.add(t.getDatetime2());
                    }
                }
            }
        }
        return list;
    }




/*
    //添加用户
    public Boolean addUser(User u){
        int insert = userMapper.insert(u);
        return insert>0;
    }

    //根据id查询-----tagInfo
    public  TagInfo selectByPrimaryKey(Integer id){
        return tagInfoMapper.selectByPrimaryKey(id);
    }

    //查询所有点名称-----taginfo
    public List<TagInfo> getTagNames(){
        return tagInfoMapper.selctTagNames();
    }

    //检查该点今天有没有数据
    public int checkCount(String tagName,String date){
        Map map = new HashMap();
        map.put("tagName",tagName);
        map.put("dateTime",date);
        map.put("comment",1);
        int count = tagInfoMapper.checkCount(map);
        System.out.println(count+"<------------count");
        return count;
    }

    //查询所有的未删除的点信息列表-----pointinfo
    public List<PointInfo> findAllPointsInfo(){
        List<PointInfo> list = new ArrayList<>();
        List<PointInfo> pointInfos = pointInfoMapper.findAllPoints();
        for (PointInfo p:pointInfos) {
            if(p.getComment()==1){
                list.add(p);
            }
        }
        return list;
    }

    public int up(PointInfo p){
      return pointInfoMapper.updateByPrimaryKey2(p);
    }

    //根据分类查询点信息3
    public List<PointInfo> checkPointInfoByKind3(String kind1){
        Map map = new HashMap<>();
        map.put("kind1",kind1);
        map.put("comment",1);
        return pointInfoMapper.findPointByKind1(map);
    }
   */
   /* //筛选
    public List<PointInfo> selectTagsByCodeOrName(String name, String code,String kind1, String kind2) {
        name=name.trim();
        code=code.trim();
        Map map = new HashMap<>();
        List<PointInfo> list = new ArrayList<>();
        if (name.isEmpty()&&code.isEmpty()){
            map.put("comment",1);
            map.put("kind2",kind2);
            if (kind2.equals("主要经营指标")||kind2.equals("主要经济指标")){
                List<PointInfo> infoList = pointInfoMapper.findPointByKind2(map);
                for (PointInfo p:infoList) {
                    if(p.getKind1().contains(kind1)){
                        p.setKind1(kind1);
                        list.add(p);
                    }
                    p.setKind1(kind1);
                    list.add(p);
                }
                return list;
            }else{
                map.put("kind1",kind1);
                return pointInfoMapper.findPointByKind2(map);
            }
        }else if(!(name.isEmpty())&&code.isEmpty()){
            map.put("comment",1);
            map.put("name",name);
            map.put("kind2",kind2);
            if (kind2.equals("主要经营指标")||kind2.equals("主要经济指标")){
                List<PointInfo> infoList = pointInfoMapper.selectTagsByName(map);
                for (PointInfo p:infoList) {
                    if(p.getKind1().contains(kind1)){
                        p.setKind1(kind1);
                        list.add(p);
                    }
                }
                return list;
            }else{
                map.put("kind1",kind1);
                return  pointInfoMapper.selectTagsByName2(map);
            }
        }else if(!(code.isEmpty())&&name.isEmpty()){
            map.put("code",code);
            map.put("comment",1);
            map.put("kind2",kind2);
            if (kind2.equals("主要经营指标")||kind2.equals("主要经济指标")){
                List<PointInfo> infoList = pointInfoMapper.selectTagsByCode(map);
                for (PointInfo p:infoList) {
                    if(p.getKind1().contains(kind1)){
                        p.setKind1(kind1);
                        list.add(p);
                    }
                }
                return list;
            }else{
                map.put("kind1",kind1);
                return  pointInfoMapper.selectTagsByCode2(map);
            }
        }else if(!(name.isEmpty())&&!(code.isEmpty())){
            map.put("name",name);
            map.put("code",code);
            map.put("comment",1);
            map.put("kind2",kind2);
            if (kind2.equals("主要经营指标")||kind2.equals("主要经济指标")){
                List<PointInfo> infoList = pointInfoMapper.selectTagsByCodeOrName(map);
                for (PointInfo p:infoList) {
                    if(p.getKind1().contains(kind1)){
                        p.setKind1(kind1);
                        list.add(p);
                    }
                }
                return list;
            }else{
                map.put("kind1",kind1);
                return  pointInfoMapper.selectTagsByCodeOrName2(map);
            }
        }
        return null;
    }

    public PointInfo findPointByCode(String pointCode) {
        return  pointInfoMapper.findPointByCode(pointCode);
    }

    public List<PointInfo> getKind_1(){
       return pointInfoMapper.findKind_1();
    }

    public List<PointInfo> getKind_2(){
        return pointInfoMapper.findKind_2();
    }

    //检查tagdataX3表格中有没有该点的数据
    public int checkCountByName(String pointname, String yesterday) {
        Map map = new HashMap();
        map.put("tagName",pointname);
        map.put("dateTime2",yesterday);
        map.put("comment",1);
        return tagDataX3Mapper.checkCountByNameAndDate(map);
    }
    //根据指标的中文意思查询指标昨天的数据
    public TagDataX3 getDataByNameAndDate(String pointname, String yesterday) {
        Map map = new HashMap();
        map.put("tagName",pointname);
        map.put("dateTime2",yesterday);
        map.put("comment",1);
        return tagDataX3Mapper.findDataByNameAndDate(map);
    }

    public List<TagDataX3> showTagNewDataByMonth(String kind1, String kind2, Integer month) {
        Map map = new HashMap();
        map.put("kind1",kind1);
        map.put("kind2",kind2);
        map.put("dateTime2",("0"+month));
        map.put("dateTime2",month.toString());
        return tagDataX3Mapper.showNewDataByKind(map);
    }
*/


}

